//
//  CemeraViewController.m
//  A段项目
//
//  Created by ～whc on 15-6-17.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "CemeraViewController.h"

@interface CemeraViewController ()

@end

@implementation CemeraViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor redColor]];
    self.navigationController.navigationBar.translucent = NO;

    
    self.navigationItem.title = @"排行榜";
    [[DateHandel shareHandle]createRankingTable];
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载中";
    [self.HUD show:YES];
    
    self.tabel = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 40) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tabel];
    self.tabel.delegate = self;
    self.tabel.dataSource = self;
    
    if (WIDTH == 320 && HEIGHT == 568) {
        self.allView = [[AllView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 260)];
    }
    if (WIDTH == 414 && HEIGHT == 736) {
        self.allView = [[AllView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 320)];
    }
    if (WIDTH == 375 && HEIGHT == 667){
        self.allView = [[AllView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 260)];
    }

    
    self.allView.allViewDelegate = self;
//    NSLog(@"self.allView = %@", self.allViewModel);
    self.tabel.tableHeaderView = self.allView;
    
    self.watchIDarray = [NSMutableArray array];
    self.array = [NSMutableArray array];
    self.number = 1;
    [self afnGetRequest];
    [self allAfnGetRequest];
    [self headerRefesh];
    [self footerRefesh];
    [_tabel autorelease];
    
}

- (void)presentAllView
{
    DetailsViewController *view = [[DetailsViewController alloc]init];
    view.hidesBottomBarWhenPushed = YES;
    view.allViewModel = self.allViewModel;
    
    if (self.allViewModel.allType == 2) {
        view.detailsID = self.allViewModel.allID;
        [self.navigationController pushViewController:view animated:YES];
        [view release];
    }
    else if (self.allViewModel.allType == 0)
    {
        TypeTowController *view1 = [[TypeTowController alloc]init];
        view1.hidesBottomBarWhenPushed = YES;
        view1.TypeID = self.allViewModel.allID;
        NSLog(@"view1 = %ld", (long)view1.TypeID);
        view1.typeName = self.allViewModel.allTitle;
        [self.navigationController pushViewController:view1 animated:YES];
    }else if (self.allViewModel.allType == 1)
    {
        WacthTVViewController *view2 = [[WacthTVViewController alloc] init];
        
        view2.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:view2 animated:YES];
        
        view2.WatID = self.allViewModel.allID;
        NSLog(@"wacth = %ld",view2.WatID);
        view2.nameStr = self.allViewModel.allTitle;
        
        [view2 release];
    }

}

//下拉刷新
-(void)headerRefesh
{
    self.tabel.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isUpLoading = NO;
        
        self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.HUD.labelText = @"正在加载中";
        [self.HUD show:YES];
        [self afnGetRequest];
        
        [self.tabel reloadData];
        [self.HUD hide:YES];
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.tabel.header endRefreshing];
        
        if (WIDTH == 320 && HEIGHT == 568) {
            self.allView = [[AllView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 260)];
        }
        if (WIDTH == 414 && HEIGHT == 736) {
            self.allView = [[AllView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 320)];
        }
        if (WIDTH == 375 && HEIGHT == 667){
            self.allView = [[AllView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 260)];
        }
        [self allAfnGetRequest];
        self.allView.allViewDelegate = self;
        //    NSLog(@"self.allView = %@", self.allViewModel);
        self.tabel.tableHeaderView = self.allView;
        [self.allView release];

        
    }];
    
    
}
//上拉加载
- (void)footerRefesh
{
    self.tabel.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
//        NSLog(@"%ld", self.number);
        self.isUpLoading = YES;
        
        self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.HUD.labelText = @"正在加载中";
        [self.HUD show:YES];
        [self afnGetRequest1];
        
        [self.tabel reloadData];
        [self.HUD hide:YES];
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.tabel.header endRefreshing];
        
        
    }];

    
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellInfer = @"myCell";
    CemeraTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInfer];
    if (cell == nil) {
        cell = [[CemeraTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellInfer];
    }
    
    Cemera *cemera = [self.array objectAtIndex:indexPath.row];
    cell.cemera = cemera;
    

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (WIDTH == 320 && HEIGHT == 568) {
        return 115;
    }
    if (WIDTH == 414 && HEIGHT == 736) {
        return 100;
    }
    if (WIDTH == 375 && HEIGHT == 667){
        return 100;
    }

    return 100;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    DetailsViewController *view = [[DetailsViewController alloc]init];
    [self.tabel deselectRowAtIndexPath:indexPath animated:YES];
    view.hidesBottomBarWhenPushed = YES;
    view.cem = [self.array objectAtIndex:indexPath.row];
    
    if (view.cem.type == 2) {
        view.detailsID = [[self.watchIDarray objectAtIndex:indexPath.row]integerValue];
        
        [self.navigationController pushViewController:view animated:YES];
        [view release];
    }
    else if (view.cem.type == 0)
    {
        TypeTowController *view1 = [[TypeTowController alloc]init];
        view1.hidesBottomBarWhenPushed = YES;
        view1.TypeID = [[self.watchIDarray objectAtIndex:indexPath.row]integerValue];
        Cemera *cem = [self.array objectAtIndex:indexPath.row];
        view1.typeName = cem.topListNameCn;
        [self.navigationController pushViewController:view1 animated:YES];
    }else if (view.cem.type == 1)
    {
        WacthTVViewController *view2 = [[WacthTVViewController alloc] init];
        
        view2.WatID = [[self.watchIDarray objectAtIndex:indexPath.row]integerValue];
        //        NSLog(@"wacth = %ld",view2.WatID);
        view2.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:view2 animated:YES];
        Cemera *cem = [self.array objectAtIndex:indexPath.row];
        view2.nameStr = cem.topListNameCn;
//        NSLog(@"view2 = %@",view2.nameStr);
        [view2 release];
    }
    
    
}



-(void)afnGetRequest
{
    self.number = 1;
    self.array = [[DateHandel shareHandle]selectRanking];
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string =@"http://api.m.mtime.cn/TopList/TopListOfAll.api?pageIndex=1";
   
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"application/x-javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        //                NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
       
        [[DateHandel shareHandle]dropRankingTable];
        [[DateHandel shareHandle]createRankingTable];
        [self.array removeAllObjects];
        
        NSMutableArray *array = [NSMutableArray array];
        array = [responseObject objectForKey:@"topLists"];
        for (NSMutableDictionary *dic in array) {
            Cemera *cem = [[Cemera alloc]init];
            cem.idd = [[dic objectForKey:@"id"]integerValue];
            NSString *string = [NSString stringWithFormat:@"%ld", cem.idd];
            cem.type = [[dic objectForKey:@"type"]integerValue];
            cem.sub = [dic objectForKey:@"summary"];
            cem.topListNameCn = [dic objectForKey:@"topListNameCn"];
            [self.array addObject:cem];
            [self.watchIDarray addObject:string];
            [[DateHandel shareHandle]insertRanking:cem];
            [cem release];
        }
        
        [self.tabel reloadData];
       
        
         [self.HUD hide:YES];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络不给力" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert autorelease];

        NSLog(@"失败==== %@",error);
         [self.HUD hide:YES];
    }];
    
}

-(void)afnGetRequest1
{
    
    self.number++;
    NSLog(@"%ld", self.number);
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string =[NSString stringWithFormat:@"http://api.m.mtime.cn/TopList/TopListOfAll.api?pageIndex=%ld", self.number];
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"application/x-javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        //                NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        
        
        NSMutableArray *array = [NSMutableArray array];
        array = [responseObject objectForKey:@"topLists"];
        for (NSMutableDictionary *dic in array) {
            Cemera *cem = [[Cemera alloc]init];
            cem.idd = [[dic objectForKey:@"id"]integerValue];
            NSString *string = [NSString stringWithFormat:@"%ld", cem.idd];
            cem.type = [[dic objectForKey:@"type"]integerValue];
            cem.sub = [dic objectForKey:@"summary"];
            cem.topListNameCn = [dic objectForKey:@"topListNameCn"];
            [self.array addObject:cem];
            [self.watchIDarray addObject:string];
            [cem release];
        }
        
        [self.tabel reloadData];
        
        
        [self.HUD hide:YES];
        [self.tabel.footer endRefreshing];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络不给力" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert autorelease];

        NSLog(@"失败==== %@",error);
        [self.HUD hide:YES];
    }];
    
}

- (void)allAfnGetRequest
{
    self.allViewModel = [[AllViewModel alloc] init];
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string = @"http://api.m.mtime.cn/PageSubArea/GetRecommendationIndexInfo.api";
//    NSLog(@"url = %@", url_string);
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"application/x-javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        //                NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        NSMutableDictionary *mineDic = [NSMutableDictionary dictionary];
        mineDic = [responseObject objectForKey:@"topList"];
        //            NSLog(@"mineDic = %@", mineDic);
        self.allViewModel.allTitle = [mineDic objectForKey:@"title"];
//        NSLog(@"title = %@", self.allViewModel.allTitle);
        self.allViewModel.allImageUrl = [mineDic objectForKey:@"imageUrl"];
        self.allViewModel.allMp4Url = [mineDic objectForKey:@"mp4Url"];
        self.allViewModel.allType = [[mineDic objectForKey:@"type"]integerValue];
        self.allViewModel.allID = [[mineDic objectForKey:@"id"]integerValue];
//        NSLog(@"self = %@", self.allViewModel);
        self.allView.AllModel = self.allViewModel;
        
        [self.tabel reloadData];
        
        
        [self.HUD hide:YES];
        [self.tabel.footer endRefreshing];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        [self.HUD hide:YES];
    }];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
